<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="老师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日报时间" prop="dailyTime">
        <el-date-picker clearable
          v-model="queryParams.dailyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:daily:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:daily:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:daily:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:daily:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dailyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老师名称" align="center" prop="teacherName" />
      <el-table-column label="日报时间" align="center" prop="dailyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dailyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日报内容" align="center" prop="dailyContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:daily:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:daily:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改老师日报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日报时间" prop="dailyTime">
          <el-date-picker clearable
                          v-model="form.dailyTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日报时间">
          </el-date-picker>
        </el-form-item>

        <div v-for="(item,index) in form.trackList" :key="index" >
          <el-row>
            <el-col :span="6">
              <el-form-item :prop="'trackList.'+ index +'.studentClass'" label="教学跟踪">
                <el-select v-model="item.studentClass" placeholder="选择班级" @change="getStudents(item.studentClass)" clearable>
                  <el-option
                    v-for="item in classes"
                    :label="item.className"
                    :value="item.id"
                    :key="item.id">
                    <span style="float: left">{{item.className }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="-" label-width="20px" :prop="'trackList.'+index+'.id'">
                <el-select v-model="item.id" placeholder="请选择学生">
                  <el-option
                    v-for="item in students"
                    :key="item.id"
                    :value="item.id"
                    :label="item.studentName">
                    <span style="float: left">{{item.studentName }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="-" label-width="15px" :prop="'trackList.'+index+'.problemType'" >
                <el-select v-model="item.problemType" placeholder="请选择问题类型">
                  <el-option v-for="dict in dict.type.stu_problem_type"
                             :key="dict.value"
                             :label="dict.label"
                             :value="parseInt(dict.value)"></el-option>
                </el-select>
              </el-form-item>

            </el-col>

            <el-col :span="5">
              <el-form-item label="-" label-width="30px" :prop="'trackList.'+index+'.startTime'" >
                <el-date-picker
                  v-model="item.startTime"
                  type="datetime"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="-" label-width="40px" :prop="'trackList.'+index+'.einMinus'" >
                <el-input v-model="item.einMinus" placeholder="扣除分数" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="17">
              <el-form-item :prop="'trackList.' + index + '.problemText'">
                <el-input v-model="item.problemText" type="textarea" autosize placeholder="情况描述"/>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"> <br> </el-col>
            <el-col :span="6">
              <el-button type="primary" @click="addTeachTrackRow" icon="el-icon-plus" >加一行</el-button>
              <el-button type="danger" @click="decTeachTrackRow(item)" icon="el-icon-minus">减一行</el-button>
            </el-col>
          </el-row>
        </div>

        <!--<div v-for="(item,index) in form.employmentList" :key="index" >
          <el-row>
            <el-col :span="6">
              <el-form-item :prop="'employmentList.'+ index +'.classId'" label="就业跟踪">
                <el-select v-model="item.classId" placeholder="选择班级"  @change="handleClass(item.classId)" clearable >
                  <el-option
                    v-for="item in classList"
                    :label="item.className"
                    :value="item.id">
                    <span style="float: left">{{item.className }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="-" label-width="20px" :prop="'employmentList.'+index+'.studentId'">
                <el-select v-model="item.studentId" placeholder="请选择学生">
                  <el-option
                    v-for="item in studentList"
                    :key="item.value"
                    :value="item.id"
                    :label="item.name">
                    <span style="float: left">{{item.name }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="-" label-width="15px" :prop="'employmentList.'+index+'.trackType'" >
                <el-select v-model="item.trackType" placeholder="请选择跟踪项目">
                  <el-option v-for="dict in dict.type.edu_track_problem_type"
                             :key="dict.value"
                             :label="dict.label"
                             :value="parseInt(dict.value)"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="-" label-width="30px" :prop="'employmentList.'+index+'.occurrenceTime'" >
                <el-date-picker
                  v-model="item.occurrenceTime"
                  type="datetime"
                  placeholder="发生时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="3">
              <el-form-item label="-" label-width="40px" :prop="'employmentList.'+index+'.employment'" >
                <el-input v-model="item.employment" placeholder="请选择工作" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="17">
              <el-form-item :prop="'employmentList.' + index + '.description'">
                <el-input v-model="item.description" type="textarea" autosize placeholder="情况描述"/>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="1"> <br> </el-col>
            <el-col :span="6">
              <el-button type="success" @click="addEmploymentRow" icon="el-icon-plus" >加一行</el-button>
              <el-button type="danger" @click="decEmploymentRow(item)" icon="el-icon-minus">减一行</el-button>
            </el-col>
          </el-row>
        </div>-->

        <el-form-item label="日报内容" prop="dailyContent">
          <el-input v-model="form.dailyContent" type="textarea" :rows="2"  placeholder="对今日工作的总结和明日工作的安排" > </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDaily, getDaily, delDaily, addDaily, updateDaily } from "@/api/teacher/daily";
import { listClass, getClass, delClass, addClass, updateClass } from "@/api/student/class";
import { listStudent,getStudents ,getStudent, delStudent, addStudent, updateStudent ,getListClasses,updateStudents,Upload} from "@/api/student/student";

export default {
  name: "Daily",
  dicts: ['stu_problem_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师日报表格数据
      dailyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      trackOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        dailyTime: null,
        dailyContent: null
      },

      flag: true,
      // 表单参数
      form: {

      },
      trackform:{},
      classes:[],
      students:[],
      // 表单校验
      rules: {
        dailyTime: [
          { required: true, message: "日报时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查询所有班级
    getClasses(){
      listClass(null).then(response => {
        this.classes=response.rows;
      });
    },
    //根据班级查询学生
    getStudents(studentClass){
      this.$refs["form"].validate(valid => {
        this.students=null;
        this.form.id=null;
        getStudents(studentClass).then(response => {
          this.students=response.data;
        });
      });

    },
    /**教学跟踪 */
    /*加一行数据 */
    addTeachTrackRow(){
      this.form.trackList.push({
        studentClass:null,
        problemText:null,
        problemType:null,
        startTime:null,
        einMinus:null,
        id:null
      });
    },
    /*减一行数据 */
    decTeachTrackRow(item){
      var index = this.form.trackList.indexOf(item)
      if (index !== -1) {
        this.form.trackList.splice(index, 1)
      }
    },
   /* /!**就业跟踪 *!/
    /!*加一行数据 TODO 这里就业跟踪后端暂时添加不了数据,1.31 *!/
    addEmploymentRow(){
      this.form.employmentList.push({
        classId:'',studentId:'',trackType:null,occurrenceTime:null,employment:null,description:null,teacherId:''
      });
    },
    /!*减一行数据 *!/
    decEmploymentRow(item){
      var index = this.form.employmentList.indexOf(item)
      if (index !== -1) {
        this.form.employmentList.splice(index, 1)
      }
    },*/
    /** 查询老师日报列表 */
    getList() {
      this.loading = true;
      listDaily(this.queryParams).then(response => {
        this.dailyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dailyId: null,
        teacherName: null,
        dailyTime: null,
        createTime: null,
        dailyContent: null,
        trackList:[
          {studentClass:null,
            problemText:null,
            problemType:null,
            startTime:null,
            einMinus:null,
            id:null}
        ],
        /*employmentList:[
          {classId:'',studentId:'',trackType:null,occurrenceTime:null,employment:null,description:null,teacherId:''}
        ]*/
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dailyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getClasses();
      this.open = true;
      this.title = "添加老师日报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dailyId = row.dailyId || this.ids
      getDaily(dailyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师日报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dailyId != null) {
            updateDaily(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDaily(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dailyIds = row.dailyId || this.ids;
      this.$modal.confirm('是否确认删除老师日报编号为"' + dailyIds + '"的数据项？').then(function() {
        return delDaily(dailyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/daily/export', {
        ...this.queryParams
      }, `daily_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
